<?php
namespace app\forumadmin_guzp\controller;
use think\facade\Config;
use think\facade\Env;
use think\facade\View;
use think\facade\Db;
use think\facade\Filesystem;
use think\facade\Cache;
class bindeal extends \app\BaseController
{
    public function binsecdeal(){
        // 检测登录状态 C组
        $this->checka();
        if(!input('find')){
            $sec = DB::view('guzp_section','s_name,s_id')
                ->view('guzp_srecycle','s_id,s_reason,recycle_time','guzp_srecycle.s_id = guzp_section.s_id')
                ->paginate([
                    'list_rows' => 5,
                    'query' => request()->param(),
            ]);
        }else{
            // 模糊查询
            $sec = DB::view('guzp_section','s_id,s_name')
            ->view('guzp_srecycle','s_id,s_reason,recycle_time','guzp_srecycle.s_id = guzp_section.s_id')
            ->where('s_name','like','%'.input('find').'%')
            ->whereor('s_reason','like','%'.input('find').'%')
            ->whereor('guzp_srecycle.s_id','like','%'.input('find').'%')
            ->paginate([
                'list_rows' => 5,
                'query' => request()->param(),
            ]);
        }
        $page = $sec->render();
        return view('',['sec' => $sec,'page' => $page]);
    }
     // 还原板块
    public function reduction(){
        // 1、将guzp_section.article_is_delete的值改为1，
        // 2、并将guzp_guzp_srecycle对应的s_id列移除。
        // 3、其板块下方所属的帖子guzp_mes.article_is_delete的值改为1,
        // 4、并将该m_id的列从 guzp_mrecycle移除。
        // 5、其所属的所有回复均将guzp_res. article_is_delete值改为1。
        // 获取板块下对应帖子的m_id
        // 获得板块下对应的所有m_id
        $mes = DB::table('guzp_mes')
            ->where('s_id',input('s_id'))
            ->field('s_id,m_id,article_is_delete')
            ->select()
            ->toArray();
        // 获得板块下所有对应的回复r_id
        $res = DB::view('guzp_mes','s_id,m_id')
            ->where('s_id',input('s_id'))
            ->view('guzp_res','r_id,m_id,article_is_delete','guzp_res.m_id = guzp_mes.m_id')
            ->select()
            ->toArray();
        // 将所有帖子article_is_delete值+2
        // 如果帖子处于正常状态即article_is_delete值为1，则在guzp_mrecycle表中插入列
        for($i = 0;$i<count($mes);$i++){
            $number = $mes[$i]['article_is_delete'];
            $mesUpate = DB::table('guzp_mes')
                ->where('m_id',$mes[$i]['m_id'])
                ->update(['article_is_delete' => $number-2]);
            if($number == 3){
                $guzp_mrecycle = DB::table('guzp_mrecycle')
                    ->where('m_id',$mes[$i]['m_id'])
                    ->delete();
            }
        }
        // 将所有回复article_is_delete的值+2
        // 如果回复处于正常状态即article_is_delete的值为1，则在guzp_resrecycle表中插入列
        for($i = 0;$i<count($res);$i++){    
            $number = $res[$i]['article_is_delete'];
            $resUpdate = DB::table('guzp_res')
                ->where('r_id',$res[$i]['r_id'])
                ->update(['article_is_delete' => $number -2]);
            if($number == 3){
                $recycle = DB::table('guzp_resrecycle')
                    ->where('r_id',$res[$i]['r_id'])
                    ->delete();
            }
        }
        // 将板块article_is_delete的值改为2
        $secycle = DB::table('guzp_section')
            ->where('s_id',input('s_id'))
            ->update(['article_is_delete' => 1 ]);
        // 在guzp_srecycle表中删除入列
        $guzp_srecycle = DB::table('guzp_srecycle')
            ->where('s_id',input('s_id'))
            ->delete();
        if($secycle && $guzp_srecycle){
            $this -> success("已还原",url('bindeal/binsecdeal'),'',3);
        }
        else{
            $this->error('放入失败');
        }
    }
    // 回收站板块彻底删除
    public function binsecdel(){
        // 1、将该板块下的所有回复删除
        // 2、将guzp_mrecycle的所有m_id对应的列移除掉，
        // 3、将guzp_mes对应的贴子全部删除，
        // 4、将guzp_guzp_srecycle对应的s_id的列移除掉
        // 5、将guzp_section对应的s_id移除掉
        $mid = DB::table('guzp_mes')
            ->where('s_id',input('s_id'))
            ->field('m_id')
            ->select();
        $mID = $mid -> toArray();
        $length = count($mID);
        // 删除板块页面下对应贴的回复信息
        for($i=0;$i<$length;$i++){
            $resresrecycle = DB::table('guzp_resrecycle')
                ->where('m_id',$mID[$i]['m_id'])
                ->delete();
            $mesRecDel = DB::table('guzp_mrecycle')
                ->where('m_id',$mID[$i]['m_id'])
                ->delete();
            $resDel = DB::table('guzp_res')
                ->where('m_id',$mID[$i]['m_id'])
                ->delete();
        }
        // 删除对应的帖子
        $mesDel = DB::table('guzp_mes')
            ->where('s_id',input('s_id'))
            ->delete();
        // 删除回收站信息
        $srecycle = DB::table('guzp_srecycle')
            ->where('s_id',input('s_id'))
            ->delete();
        // 删除板块
        $secDel = DB::table('guzp_section')
            ->where('s_id',input('s_id'))
            ->delete();
        if($secDel){
            $this -> success("删除成功",url('bindeal/binsecdeal'),'',3);
        }
        else{
            $this->error('删除失败');
        }
    }

    // 帖子回收站管理
    public function binmesdeal(){
        // 检测登录状态 C组
        $this->checka();
        if(!input('find')){
            $mes = DB::view('guzp_mes','m_title,m_id,m_createat,u_nick,article_is_delete')
                ->view('guzp_mrecycle','m_id,m_reason,mrecycle_time','guzp_mrecycle.m_id = guzp_mes.m_id')
                ->paginate([
                    'list_rows' => 5,
                    'query' => request()->param(),
            ]);
        }else{
            // 模糊查询
            $mes = DB::view('guzp_mes','m_title,m_id,m_createat,u_nick,article_is_delete')
                ->view('guzp_mrecycle','m_id,m_reason,mrecycle_time','guzp_mrecycle.m_id = guzp_mes.m_id')
            ->where('u_nick','like','%'.input('find').'%')
            ->whereor('m_title','like','%'.input('find').'%')
            ->whereor('guzp_mrecycle.m_id','like','%'.input('find').'%')
            ->paginate([
                'list_rows' => 5,
                'query' => request()->param(),
            ]);
        }
        $page = $mes->render();
        return view('',['mes' => $mes,'page' => $page]);
    }
    // 帖子还原管理
    public function mesDuction(){
        $res = DB::table('guzp_res')
            ->where('m_id',input('m_id'))
            ->where('article_is_delete','>',1)
            ->field('article_is_delete,r_id,m_id')
            ->select()
            ->toArray();
        $length = count($res);
        for($i = 0;$i<$length;$i++){
            $number = $res[$i]['article_is_delete'];
            if($number == 3  ){
                $resrecycle = DB::table('guzp_resrecycle')
                    ->where('r_id',$res[$i]['r_id'])
                    ->delete();
                $resY = DB::table('guzp_res')
                ->where('r_id',$res[$i]['r_id'])
                ->update(['article_is_delete' => 1]);
            }
            elseif($number == 4 ){
                $resY = DB::table('guzp_res')
                ->where('r_id',$res[$i]['r_id'])
                ->update(['article_is_delete' => 2]);
            }
        }
        $mes = DB::table('guzp_mes')
            ->where('m_id',input('m_id'))
            ->update(['article_is_delete' => 1]);
        $domesRecycle = DB::table('guzp_mrecycle')
            ->where('m_id',input('m_id'))
            ->delete();
        if($domesRecycle && $mes){
            $this->success('帖子还原成功','bindeal/binmesdeal');
        }
        else{
            $this->error('帖子还原失败');
        }
        
    }
    // 帖子彻底删除管理
    public function binmesdel(){
        $resresrecycle = DB::table('guzp_resrecycle')
                ->where('m_id',input('m_id'))
                ->delete();
        $resDel = DB::table('guzp_res')
            ->where('m_id',input('m_id'))
            ->delete();
        // 删除帖子回收站信息
        $srecycle = DB::table('guzp_mrecycle')
            ->where('m_id',input('m_id'))
            ->delete();
        // 删除对应的帖子
        $mesDel = DB::table('guzp_mes')
            ->where('m_id',input('m_id'))
            ->delete();
        if($mesDel && $srecycle){
            $this->success('已将该贴彻底删除','bindeal/binmesdeal');
        }
        else{
            $this->error('删除失败');
        }
    }
    // 回复回收站管理
    public function binresdeal(){
        // 检测登录状态 C组
        echo(input('find'));
        $this->checka();
        if(!input('find')){
            $res = DB::view('guzp_mes','m_id,m_title')
                ->view('guzp_res','m_id,r_id,article_is_delete,r_content,u_nick,r_createat','guzp_mes.m_id = guzp_res.m_id')
                ->view('guzp_resrecycle','r_id,r_reason,rrecycle_time','guzp_resrecycle.r_id = guzp_res.r_id')
                ->paginate([
                    'list_rows' => 5,
                    'query' => request()->param(),
            ]);
        }else{
            // 模糊查询
            $res = DB::view('guzp_mes','m_id,m_title')
                ->view('guzp_res','r_id,r_content,u_nick,r_createat,article_is_delete','guzp_mes.m_id = guzp_res.m_id')
                ->view('guzp_resrecycle','r_id,r_reason,rrecycle_time','guzp_resrecycle.r_id = guzp_res.r_id')
                ->where('m_title','like','%'.input('find').'%')
                ->whereor('guzp_res.u_nick','like','%'.input('find').'%')
                ->whereor('guzp_mes.m_id','like','%'.input('find').'%')
                ->whereor('r_reason','like','%'.input('find').'%')
                ->paginate([
                    'list_rows' => 5,
                    'query' => request()->param(),
                ]);
                
        }
        $page = $res->render();
        return view('',['res'=>$res,'page' => $page]);
    }
    // 还原回复
    public function resReduction(){
        $resrecycle = DB::table('guzp_resrecycle')
            ->where('r_id',input('r_id'))
            ->delete();
        $res = DB::table('guzp_res')
            ->where('r_id',input('r_id'))
            ->update(['article_is_delete' => 1]); 
        if($res && $resrecycle){
            $this->success('还原回复成功','bindeal/binresdeal');
        }
        else{
            $this->error('还原回复失败','bindeal/binresdeal');
        }
        // 判断该回复被放入回收站的原因，如果回复贴的article_is_delete = 0或1，则可
    }
    // 彻底删除回复
    public function binresdel(){
        $resrecycle = DB::table('guzp_resrecycle')
            ->where('r_id',input('r_id'))
            ->delete();
        $res = DB::table('guzp_res')
            ->where('r_id',input('r_id'))
            ->delete();
        if($resrecycle && $res){
            $this -> success('删除成功','bindeal/binresdeal');
        }
        else{
            $this->error('删除失败','bindeal/binresdeal');
        }
    }
}